<?php

    function connect($config)
    {
      try  
      {
          $connection =
          new PDO($config['driver'].":host=".
          $config['host'].";dbname=".
          $config['dbname'], $config['user'], $config['pass']);
          
          return $connection;  
      }
      catch(PDOException $e)
      {
         return false;
      }
        
    }
    
    function query($queryString, $bindParam, $connection)
    {
        $state = $connection->prepare($queryString);
        $state->execute($bindParam);
        $res = $state->fetchAll();
        if($res)
            return $res;
        else
            return false; 
        
        //if($state->rowCount() > 0)
        //    return $state;
        //else
        //    return false;    
    }
    
    function getAllPosts($connection, $limit = 0,$limit1 = 10)
    {
      try  
      {
         $state =  $connection->query("SELECT * FROM titles 
                ORDER BY PublicDate DESC LIMIT $limit,$limit1");
           if($state->rowCount() > 0)
            return $state->fetchAll();
           else
            return false;    
      }
      catch(PDOException $e)
      {
         return false;
      } 
    }
    
    
    function getPostById($id, $connection)
    {
        $result = query("SELECT * FROM titles WHERE TitleId = :id LIMIT 1",
        array('id'=>$id), $connection);
        if($result)
          return $result[0];
    }
    
    
    function view($path , $data)
    {
        $path.=".view.php";
        
        if($data)
            extract($data);
        
        include_once "views/main.view.php"; 
    }
    
     function getAllComments($connection, $comm, $limit = 10)
    {
         $state =  $connection->query("SELECT * FROM comments WHERE TitleID = $comm 
                ORDER BY Time DESC LIMIT $limit");
           if($state->rowCount() > 0)
            return $state->fetchAll();
           else
            return false;    
    }
    
    
    function getAll($connection)
    {
      try  
      {
         $state =  $connection->query("SELECT * FROM titles");
           if($state->rowCount() > 0)
            return $state->fetchAll();
           else
            return false;    
      }
      catch(PDOException $e)
      {
         return false;
      } 
    }
    


?>